﻿Public Class frmMObjetivows
    Public arrConsulta(1) As String
    Private arrCampos() As String = {"objetivo_id", "objetivo_nombre"}

    Private Sub frmMObjetivows_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mdiPrincipal.tsmiObjetivows.Enabled = True
    End Sub

    Private Sub frmMObjetivows_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        refreshList()
    End Sub

    Private Sub tsbNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbNuevo.Click
        Me.tsbNuevo.Enabled = False
        Me.renderForm("Nuevo - Objetivo", 1)
    End Sub

    Private Sub tsbConsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbConsultar.Click
        Me.tsbConsultar.Enabled = False
        Me.renderForm("Consultar - Objetivo", 2)
    End Sub

    Private Sub tsbEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbEditar.Click
        Dim strId As String = Me.dgvObjetivo.Rows(Me.dgvObjetivo.CurrentRow.Index).Cells(0).Value.ToString()

        If (strId <> "") Then
            Me.tsbEditar.Enabled = False
            renderForm("Editar - Objetivo", 3)
        Else
            MsgBox("Debe seleccionar un registro", MsgBoxStyle.Information, "Objetivo")
        End If
    End Sub

    Private Sub tsbBorrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbBorrar.Click
        Dim objResult = MsgBox("Esta seguro que desea borrar el registro seleccionado?", MsgBoxStyle.YesNo, "Borrar - Objetivo")

        If (objResult = DialogResult.Yes) Then
            Dim strId As String = Me.dgvObjetivo.Rows(Me.dgvObjetivo.CurrentRow.Index).Cells(0).Value.ToString

            mdlConexion.blnOnDB = True
            mdlCRUD.delRecord("objetivo", "objetivo_id = '" & strId & "'")
            mdlConexion.blnOnDB = False

            If (mdlCRUD.blnFlag = True) Then
                Me.refreshList()
            End If
        End If
    End Sub

    Private Sub tsbRecargar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbRecargar.Click
        arrConsulta(0) = ""
        arrConsulta(1) = ""
        Me.refreshList()
    End Sub

    Private Sub renderForm(ByVal strTitle As String, ByVal intMode As Integer)
        Dim frmObjetivo As New frmObjetivows

        frmObjetivo.MdiParent = mdiPrincipal
        frmObjetivo.frmParent = Me

        frmObjetivo.modeForm = intMode
        frmObjetivo.Text = strTitle
        frmObjetivo.Show()

    End Sub

    Public Sub refreshList()
        Dim dstRecord As New DataSet
        Dim strCondicion As String = ""
        dstRecord.Tables.Add("Objetivo")

        For i As Integer = 0 To 1
            If (Me.arrConsulta(i) <> "") Then
                strCondicion &= IIf(strCondicion <> "", " AND ", "") & IIf(i <> 1, Me.arrCampos(i) & " = '" & LCase(Me.arrConsulta(i)) & "'", "LOWER(" & Me.arrCampos(i) & "::text) LIKE '%" & LCase(Me.arrConsulta(i)) & "%'")
            End If
        Next

        mdlConexion.blnOnDB = True
        mdlCRUD.loadRecord(dstRecord, "Objetivo", "objetivo", strCondicion, "objetivo_id")
        mdlConexion.blnOnDB = False

        Me.dgvObjetivo.DataSource = dstRecord.Tables("Objetivo")

        Me.dgvObjetivo.Columns(0).HeaderText = "Codigo"
        Me.dgvObjetivo.Columns(1).HeaderText = "Objetivo"
        Me.dgvObjetivo.Columns(0).Width = 60
        Me.dgvObjetivo.Columns(1).Width = 360
    End Sub
End Class